package com.zhiwang.planet.ui.my

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.SystemClock
import android.provider.MediaStore
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.top.zibin.luban.Luban
import com.top.zibin.luban.OnCompressListener
import com.zhiwang.planet.R
import com.zhiwang.planet.databinding.ActivityFeedBack2Binding
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvvm.viewmodel.entertain.PlayGameConnection
import com.zhiwang.planet.mvvm.viewmodel.my.FeedBackVMConnection
import com.zhiwang.planet.mvvm.viewmodel.my.FeedBackViewModel
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.common.NodeCoordinate
import com.zhiwang.planet.ui.common.PhotoPreviewActivity
import com.zhiwang.planet.util.Toaster
import kotlinx.android.synthetic.main.activity_feed_back2.*
import java.io.File
import javax.inject.Inject

class FeedBack2Activity : BaseActivity() {


    @Inject
    lateinit var feedBack2ViewModel: FeedBackViewModel

    override fun initInjection() {
        DaggerUserComponent.builder()
                .accountModule(AccountModule(this))
                .appComponent(getAppComponent())
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<ActivityFeedBack2Binding>(this, R.layout.activity_feed_back2)
        setSupportActionBar(toolbar)
        binding.vm = feedBack2ViewModel
        binding.lifecycleOwner = this
        feedBack2ViewModel.errHandler = this::onError

        feedBack2ViewModel.loadAdviceList()

        FeedBackVMConnection.addImageEvent.observe(this, Observer {
            if (it)
                startActivityForResult(galleryIntent(), 10000)
        })

        FeedBackVMConnection.previewImageEvent.observe(this, Observer {
            PhotoPreviewActivity.load(this, it.path, NodeCoordinate(0, 0, 0, 0))
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 10000) {
            val imageUri = data?.data
            if (imageUri != null)
                compress(imageUri)
        }
    }

    private fun compress(imageUri: Uri) {
        Luban.with(this)
                .load(imageUri)
                .setTargetDir(cacheDir.path)
                .setRenameListener {
                    return@setRenameListener SystemClock.uptimeMillis().toString()
                }
                .setCompressListener(object : OnCompressListener {

                    override fun onSuccess(file: File) {
                        FeedBackVMConnection.imgesToUpload.add(file)
                        FeedBackVMConnection.onImageAddEvent.value = file
                    }

                    override fun onError(e: Throwable?) {

                        Toaster.message(e?.message ?: "图片压缩错误")
                    }

                    override fun onStart() {

                    }

                }).launch()
    }


    private fun galleryIntent(): Intent {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
        return intent
    }

    override fun onDestroy() {
        super.onDestroy()
        PlayGameConnection.reset()
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }
}
